// pages/login/login.js
const CONFIG = require('../../config.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    password: "",
    usernameFocus: true,
    passwordFocus: false,
    isLoaded: false,
    isDisposed: true,
    navigation_title: "登录",
    is_back: true,
    iv_path: "/img/detail_back.png",
    panid: 1
  },
  login: function(e) {
    //JSESSIONID=ABDF18CC16CB6E9AE7ECF2A376928C58;
    wx.showLoading({
      "mask": true
    })
    let url = "/static/pages/menu/index";
    wx.request({
      url: CONFIG.subDomain + "weChatAction/webLogin.do",
      data: {
        'username': this.data.username, // 设置微信的appid
        'password': this.data.password,

      },
      success: (res) => {
        console.log("data---" + res.data)
        if (res.data.success) {
          wx.hideLoading();
          //app.globalData.chcToken = res.data.chcToken;
          wx.setStorageSync("checkToken", res.data.chcToken);
          wx.switchTab({
            url: '/pages/check/check'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 3000
          })
        }
      },
      fail: (res) => {
        wx.switchTab({
          url: '/pages/check/check'
        })
      }
    })
  },
  onChangeUsernName(e) {
    const result = e.detail;
    this.setData({
      username: result
    })
    console.log(result);
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      if (!this.data.isDisposed) {
        this.setData({
          isDisposed: true
        })
      }
    } else {
      if (this.data.isDisposed) {
        this.setData({
          isDisposed: false
        })
      }
    }
  },
  onChangePassWord(e) {
    const result = e.detail;
    this.setData({
      password: result
    })
    console.log(result);
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      if (!this.data.isDisposed) {
        this.setData({
          isDisposed: true
        })
      }
    } else {
      if (this.data.isDisposed) {
        this.setData({
          isDisposed: false
        })
      }
    }
  },
  onChange(e) {
    console.log("username:" + this.data.username);
    console.log("password:" + this.data.password);
    if (this.data.username.length == 0 || this.data.password.length == 0) {
      if (!this.data.isDisposed) {
        this.setData({
          isDisposed: true
        })
      }
    } else {
      if (this.data.isDisposed) {
        this.setData({
          isDisposed: false
        })
      }
    }
  },
  bindWeixin(e) {
    wx.showLoading({
      "mask": true
    })
    wx.request({
      url: CONFIG.subDomain + "weChatAction/weChatBind.do",
      data: {
        "openID": "gh_d17c225d5ec3",
        "userOpenID":app.globalData.userInfo.openId,
        'username': this.data.username, // 设置微信的appid
        'password': this.data.password,

      },
      success: (res) => {
        console.log("data---" + res.data)
        if (res.data.success) {
          wx.hideLoading();
          wx.setStorageSync("checkToken", res.data.chcToken);
          wx.switchTab({
            url: '/pages/check/check'
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 3000
          })
          
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 3000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }


})